import { Controller } from './http.js'

class Webclient extends Controller {
  constructor(...args) {
    super(...args)
  }

  async version_info() {
    const url = '/web/webclient/version_info'
    return this.json_call(url, {})
  }
}

class Database extends Controller {
  constructor(...args) {
    super(...args)
  }

  async list() {
    const url = '/web/database/list'
    return this.json_call(url, {})
  }
}

class Session extends Controller {
  constructor(...args) {
    super(...args)

    this._session_info = {}
  }

  get session_info() {
    // todo,
    // odoo15 以后 user_companies 的格式改变
    //
    // odoo15 以后:
    // user_companies.current_company 是 id
    // user_companies.allowed_companies 是 [{id,name},{id,name}]
    //
    // odoo14 以前:
    // user_companies.current_company 是 [id,name]
    // user_companies.allowed_companies 是 [[id,name],[id,name]]
    //
    // {
    //   server_version_info: [17, 0, 0, ...],
    //   user_companies:{
    //     allowed_companies: [{id, name},], // [[id, name], ...]
    //     current_company: id,  // [id, name]
    //   }
    // }

    return this._session_info
  }

  set session_info(info) {
    this._session_info = info
  }

  get user_context() {
    const { user_context = {} } = this.session_info
    return user_context
  }

  get lang() {
    const { lang = 'en_US' } = this.user_context
    return lang
  }

  get current_company_id() {
    const { user_companies = {} } = this.session_info
    const { current_company } = user_companies
    return current_company
  }

  get company() {
    return {
      id: this.current_company_id
    }
  }

  get cids_odoo() {
    const info = this.session_info || {}
    const { user_companies = {} } = info
    const { allowed_companies = {} } = user_companies

    const cids_odoo = Object.values(allowed_companies).map(item => item.id)
    return cids_odoo
  }

  get_cids_local(cids_odoo) {
    return this._jsonrpc.get_cids_local(cids_odoo)
  }

  get cids_local() {
    const cids_odoo = this.cids_odoo
    return this.get_cids_local(cids_odoo)
  }

  set_cids_local(cids) {
    return this._jsonrpc.set_cids_local(cids)
  }

  set cids_local(cids) {
    return this.set_cids_local(cids)
  }

  get_session_id() {
    return this._jsonrpc.get_session_id()
  }

  async authenticate(payload) {
    const url = '/web/session/authenticate'
    const info = await this.json_call(url, payload)
    this._session_info = info
    return info
  }

  async get_session_info() {
    const url = '/web/session/get_session_info'
    const info = await this.json_call(url, {})
    this._session_info = info
    return info
  }

  async check() {
    const url = '/web/session/check'
    return await this.json_call(url, {}, { throw_error: 1 })
  }

  async destroy() {
    const url = '/web/session/destroy'
    await this.json_call(url, {})
    this._session_info = undefined
    return true
  }

  async logout() {
    try {
      await this.destroy()
      return true
    } catch (error) {
      return false
    }
  }

  async session_check() {
    const my_check = async () => {
      try {
        await this.check()
        return true
      } catch (error) {
        return false
      }
    }

    const res = await my_check()
    if (res) {
      return await this.get_session_info()
    } else {
      return res
    }
  }

  async modules() {
    const url = '/web/session/modules'
    return this.json_call(url, {})
  }
}

class Dataset extends Controller {
  constructor(...args) {
    super(...args)
  }

  async call_kw(model, method, args, kwargs) {
    const url = '/web/dataset/call_kw'
    const url2 = `${url}/${model}/${method}`
    return this.json_call(url2, { model, method, args, kwargs })
  }

  async _raw_call_button(model, method, args, kwargs) {
    const url = '/web/dataset/call_button'
    // console.log(model, method, args, kwargs)
    return this.json_call(url, { model, method, args, kwargs })
  }

  async call_button(model, method, args, kwargs) {
    try {
      const result = await this._raw_call_button(model, method, args, kwargs)
      return { result }
    } catch (error) {
      return false
    }
  }
}

class Home extends Controller {
  constructor(...args) {
    super(...args)
    this.webclient = new Webclient(...args)
    this.database = new Database(...args)
    this.session = new Session(...args)
    this.dataset = new Dataset(...args)
  }
}

export const Web = Home
